#include "hzfrp.h"
#include <QProcess>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QNetworkInterface>


using namespace Moolink;


void HZFRP::conncent(const QSettings* settings)
{
    //QString host = settings->value("host").toString();
    quint16 port=settings->value("port").toUInt() & 0xFFFF;

    QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
    int nCnt = nets.count();
    QString strMacAddr = "";
    for(int i = 0; i < nCnt; i ++)
    {
        // 如果此网络接口被激活并且正在运行并且不是回环地址，则就是我们需要找的Mac地址
        if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning)
                && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            strMacAddr = nets[i].hardwareAddress().replace(":","").toLower();
            break;
        }
    }


    #if defined(Q_OS_WIN32)

        QString path=QDir::currentPath();//获取程序当前工作目录
        path.replace("/","\\");//将地址中的"/"替换为"\"，因为在Windows下使用的是"\"
        //    if(!QProcess::startDetached(path+"\\frpc.exe http -s frphz.moolink.ltd:27000 -l 8080 --sd jxat"))//启动主程序，主程序在其上一级目录
        path+="\\frpc.exe http -s frphz.moolink.ltd:27000 -l "+QString::number(port,10)+" --sd "+strMacAddr;

        if(!QProcess::startDetached(path))//启动frp
        {
//           qDebug()<<"FRP启动失败,将无法远程访问!\r\n"<<path;
            //QProcess::startDetached("explorer http://localhost:"+QString::number(port,10));
            QProcess::startDetached("explorer http://localhost:"+QString::number(port,10)+"/geturl");
        }
        else
        {
          // qDebug()<<"启动FRP成功,远程访问地址 http://"+strMacAddr+".hz.zjvo.cn:20080"<<"\r\n";
           //QProcess::startDetached("explorer http://"+strMacAddr+".hz.zjvo.cn:20080");
           QProcess::startDetached("explorer http://localhost:"+QString::number(port,10)+"/geturl/"+strMacAddr);

        }

    #elif defined(Q_OS_LINUX)

        //QProcess::startDetached("explorer http://localhost:"+QString::number(port,10));

    #endif
}
